package com.example.char04;

/**
 * Created by Administrator on 2017/6/22.
 */

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.DocumentsContract;
import android.widget.Toast;

import com.stericson.RootTools.RootTools;


/**业务工具类*/
public class EngineUtils {
    /**分享应用*/
    public  static void  shareApplication(Context context, AppInfo appInfo){
        Intent intent=new Intent("android.intent.action.SEND");
        intent.addCategory("android.intent.category.DEFAAUT");
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT,"推荐你使用一款软件,名称叫："+appInfo.appName+
       " 下载路径：http://play.google.com/store/apps/details?id="+appInfo.PackageName);
        context.startActivity(intent);
    }
    /**开启应用程序*/
    public  static void  startApplication(Context context, AppInfo appInfo) {
        //打开这个应用程序的入口activity
        PackageManager pm = context.getPackageManager();
        Intent intent = pm.getLaunchIntentForPackage(appInfo.appName);
        if (intent != null) {
            context.startActivity(intent);
        } else {
            Toast.makeText(context, "该应用没有启动界面", Toast.LENGTH_LONG).show();

        }
    }
        /**开启应用界面设置页面*/
        public  static void  SettingAppDetail(Context context, AppInfo appInfo) {
            Intent intent = new Intent();
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            intent.setAction("androoid.settings.APPLICATION_DETAALLS_SETTINGS");

            intent.setData(Uri.parse("package:" + appInfo.PackageName));
            context.startActivity(intent);
        }
          /**卸载应用*/
    public  static void  uninstallApplication(Context context, AppInfo appInfo){
        if(appInfo.isUserApp) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_DELETE);
            intent.setData(Uri.parse("package:" + appInfo.PackageName));
            context.startActivity(intent);

    }else{
        //系统应用需要root权限，利用Linux命令删除文件
      //  if(!RootTools.isRootAvallable()){
          //  Toast.makeText(context,"卸载系统应用，必须要root权限",Toast.LENGTH_LONG).show();
           // return;
        }
        try{
            if (!RootTools.isAccessGiven()){
                Toast.makeText(context,"请授权黑马小护卫root权限",Toast.LENGTH_LONG).show();
                return;
            }
            //RootTools.sendShell("mount-o remount ,rw/system",3000);
            //RootTools.sendShell("rm -r "+appInfo.apkPath,3000);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    }

